<?php
defined('BASEPATH') or exit('No direct script access allowed');
require APPPATH.'libraries/BaseApiController.php';
class classname extends  BaseApiController
{
   public function index(){

   }
   
   public function list_by_page(){
   	   $token=$this->verifyToken();
   	   if($token){
   		   $keys=[
   		   		["title","option","班级名称",""],
				["teacher","option","授课老师",""],
   		   		["course","option","关联课程",""],
   		   ];
   		   
   		   $data=$this->getFields($keys);
   		   
   		    
   		   $this->db->where("schcode",$token["schcode"]);
   		   $res=$this->db->select("count(*) as count")->get("class")->row_array();
   		   $numrows=(int)$res["count"];
   		   
   		    
   		   $this->db->where("schcode",$token["schcode"]);
   		   $rows=$this->db->get("class")->result_array();
   		   
   		   for($i=0;$i<count($rows);$i++){
 			   $rows[$i]["teachers"]=json_decode($rows[$i]["teachers"],JSON_UNESCAPED_UNICODE);
			    $rows[$i]["courses"]=json_decode($rows[$i]["courses"],JSON_UNESCAPED_UNICODE);
   		   }		   
   		   
   		   $this->WRITE("code",1);
   		   $this->WRITE("numrows",$numrows);
   		   $this->WRITE("list",$rows);
   		   $this->RESPONSE();	 
   		   
   	   }
   	 
   }
   
   public function create(){
   	   $token=$this->verifyToken();
   	   if($token){
   		   $keys=[
   		   		 ["title","required","班级名称",""],
   		   		 ["capacity","required","班级容量",""],
   		   		 ["courses","required","课程",[]],
   		   		 ["teachers","option","授课老师",[]],
   		   		 ["room","option","上课教室",""],
   		   		 ["memo","option","备注",""],
   		   ];
   		   $data=$this->getFields($keys);
   		   $data["schcode"]=$token["schcode"];
		   
		   $teacher_ids=[];
		   foreach($data["courses"] as $course){
			   var_dump($course);
		   }
		   
		   
		   
		   $data["courses"]=json_encode($data["courses"],JSON_UNESCAPED_UNICODE);
		   $data["teachers"]=json_encode($data["teachers"],JSON_UNESCAPED_UNICODE);
   		   
   		   $res=$this->db->insert("class",$data);
   		   if($res){
   		   		   $this->WRITE("code",1);
   		   		   $this->WRITE("msg","新增课程成功!");
   		   		   $this->RESPONSE();	
   		   }else{
   		   		   $this->WRITE("code",0);
   		   		   $this->WRITE("msg","新增课程失败!");
   		   		   $this->RESPONSE();	
   		   }   
   	   }
   }
   
   public function update(){
   	   $token=$this->verifyToken();
   	   if($token){
   		   $keys=[
			     ["classid","required","班级ID",""],
   		   		 ["title","required","班级名称",""],
   		   		 ["capacity","required","班级容量",""],
   		   		 ["courses","option","课程",[]],
   		   		 ["teachers","option","授课老师",[]],
   				 ["room","option","上课教室",""],
   				 ["memo","option","备注",""],
   		   ];
   		   $data=$this->getFields($keys);
		   
		   $course_ids=[];
		   foreach($data["courses"] as $course){
		   	  array_push($course_ids,$course["courseid"]);
		   }
		   $data["course_ids"]=json_encode($course_ids,JSON_UNESCAPED_UNICODE);
		   
		   
		   $teacher_ids=[];
		   foreach($data["teachers"] as $teacher){
		   	  array_push($teacher_ids,$teacher["tid"]);
		   }
		   $data["teacher_ids"]=json_encode($teacher_ids,JSON_UNESCAPED_UNICODE);
		   
		   
		   
    	   $data["courses"]=json_encode($data["courses"],JSON_UNESCAPED_UNICODE);
   		   $data["teachers"]=json_encode($data["teachers"],JSON_UNESCAPED_UNICODE);
   		   
		   
   		   $res=$this->db->where("classid",$data["classid"])->update("class",$data);
   		   if($res){
   		   		   $this->WRITE("code",1);
   		   		   $this->WRITE("msg","更新成功!");
   		   		   $this->RESPONSE();	
   		   }else{
   		   		   $this->WRITE("code",0);
   		   		   $this->WRITE("msg","更新失败!");
   		   		   $this->RESPONSE();	
   		   }   
   	   }
   }
   
   
   public function delete(){
   	   $token=$this->verifyToken();
   	   if($token){
   	   $data=$this->getFields(array(["classid","required","班级索引",""]));
   	   
   	   $res=$this->db->where("classid",$data["classid"])->delete("class",$data);
   	   if($res){
   		   $this->WRITE("code",1);
   		   $this->WRITE("msg","删除成功!");
   		   $this->RESPONSE();	
   	   }else{
   		   $this->WRITE("code",0);
   		   $this->WRITE("msg","删除失败!");
   		   $this->RESPONSE();	
   	   }
	   }
   
   }
   
   public function detail(){
	   $token=$this->verifyToken();
	   if($token){
		   $data=$this->getFields(array(["classid","required","班级索引",""]));
		   $res=$this->db->where("classid",$data["classid"])->get("class")->row_array();
		   
		   $res["courses"]=json_decode($res["courses"],JSON_UNESCAPED_UNICODE);
		   $res["teachers"]=json_decode($res["teachers"],JSON_UNESCAPED_UNICODE);
		   $res["teacher_ids"]=json_decode($res["teacher_ids"],JSON_UNESCAPED_UNICODE);
		   $res["course_ids"]=json_decode($res["course_ids"],JSON_UNESCAPED_UNICODE);
		   if($res){
			   $this->WRITE("code",1);
			   $this->WRITE("data",$res);
			   $this->WRITE("msg","查询成功!");
			   $this->RESPONSE();	
		   }else{
			   $this->WRITE("code",0);
			   $this->WRITE("msg","操作失败!");
			   $this->RESPONSE();	
		   }
	    }
   }
}